#include "setupwindow.h"
#include "ui_setupwindow.h"

#include <QMessageBox>
#include <QString>
#include "Player.h"

SetupWindow::SetupWindow(QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::SetupWindowClass),
    m_numPlayers(0)
{
   m_ui->setupUi(this);
   connect(m_ui->playSelect, SIGNAL(valueChanged(int)), this, SLOT(selectNumPlayers(int)));
   connect(m_ui->playGameButton, SIGNAL(clicked()), this, SLOT(handleButtonClick()));
   m_playLabels.push_back(m_ui->play1);
   m_playLabels.push_back(m_ui->play2);
   m_playLabels.push_back(m_ui->play3);
   m_playLabels.push_back(m_ui->play4);
   m_playLabels.push_back(m_ui->play5);
   m_playLabels.push_back(m_ui->play6);
   m_playNames.push_back(m_ui->playName1);
   m_playNames.push_back(m_ui->playName2);
   m_playNames.push_back(m_ui->playName3);
   m_playNames.push_back(m_ui->playName4);
   m_playNames.push_back(m_ui->playName5);
   m_playNames.push_back(m_ui->playName6);
   m_playColors.push_back(m_ui->play1Color);
   m_playColors.push_back(m_ui->play2Color);
   m_playColors.push_back(m_ui->play3Color);
   m_playColors.push_back(m_ui->play4Color);
   m_playColors.push_back(m_ui->play5Color);
   m_playColors.push_back(m_ui->play6Color);
   for(unsigned int i = 2; i < m_playLabels.size(); i++)
   {
       m_playLabels.at(i)->setDisabled(true);
       m_playNames.at(i)->setDisabled(true);
       m_playColors.at(i)->setDisabled(true);
   }
   m_numPlayers = m_ui->playSelect->value();
}

SetupWindow::~SetupWindow()
{
    delete m_ui;
}

void SetupWindow::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SetupWindow::selectNumPlayers(int value)
{
    if(value < m_numPlayers)
    {
        for(int i = m_numPlayers - 1; i > value - 1; i--)
        {
            m_playLabels.at(i)->setDisabled(true);
            m_playNames.at(i)->setDisabled(true);
            m_playColors.at(i)->setDisabled(true);
        }
    }
    else
    {
        for(int i = 0; i < value; i++)
        {
            m_playLabels.at(i)->setEnabled(true);
            m_playNames.at(i)->setEnabled(true);
            m_playColors.at(i)->setEnabled(true);
        }
    }
    m_numPlayers = value;
}

void SetupWindow::handleButtonClick()
{
    if(checkPlaySetup())
    {
        startGame();
    }
}

bool SetupWindow::checkPlaySetup()
{
    for(int i = 0; i < m_numPlayers; i++)
    {
        if(m_playNames.at(i)->text() == "")
        {
            QMessageBox msg;
            msg.setText(QString("I'm sorry, one player has not entered a name"));
            msg.exec();
            return false;
        }
        for(int j = 0; j < m_numPlayers; j++)
        {
            if(i == j)
                continue;
            else
            {
                if(m_playColors.at(i)->currentText() == m_playColors.at(j)->currentText())
                {
                    QMessageBox msg;
                    msg.setText(QString("I'm sorry, at least two players have the same color"));
                    msg.exec();
                    return false;
                }
            }
        }
    }
    return true;
}

void SetupWindow::startGame()
{
    for(int i = 0; i < m_numPlayers; i++)
    {
        Player* play = new Player(m_playColors.at(i)->currentText(), m_playNames.at(i)->text());
        m_players.push_back(play);
    }
    emit playersCreated(m_players);
    this->close();
}
